package GUI.TabPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CheckInPanel extends JPanel {
	private JLabel bidLabel = new JLabel("Booking ID:");
	private JTextField bidText = new JTextField(9);

	private JLabel firstNameLabel = new JLabel("First Name:");
	private JTextField firstNameText = new JTextField(9);

	private JLabel lastNameLabel = new JLabel("Last Name:");
	private JTextField lastNameText = new JTextField(9);

	private JLabel emailLabel = new JLabel("Email:");
	private JTextField emailText = new JTextField(9);

	private JLabel blank = new JLabel(" ");
	private JButton search;
	private JButton confirm;
	private JButton cancel;

	private JTable informationTable;

	//TableRowSorter<DefaultTableModel> sorter;

	private String[] columnNames =
	{
			"Booking #", "First Name", "Last Name", "Email",
			"Room #", "Date In", "Date Out", "Cost", "Status", "Hotel Name"
	};

	public CheckInPanel(){
		this.setLayout(new BorderLayout());

		JPanel west = new JPanel();
		JPanel center = new JPanel();
		JPanel east = new JPanel();

		west.setBackground(new Color(147, 37, 37));
		west.add(new JLabel("      "));
		east.setBackground(new Color(147, 37, 37));
		east.add(new JLabel("      "));

		center.setLayout(new GridLayout(0, 2, -300, 7));

		search = new JButton("Search");		 
		confirm = new JButton("Check In");
		cancel = new JButton("Cancel Booking");

		center.add(bidLabel);
		center.add(bidText);
		center.add(firstNameLabel);
		center.add(firstNameText);
		center.add(lastNameLabel);
		center.add(lastNameText);
		center.add(emailLabel);
		center.add(emailText);
		center.add(blank);

		search.addActionListener(new SearchButton());
		confirm.addActionListener(new ConfirmButton());
		cancel.addActionListener(new CancelButton());

		JPanel buttons = new JPanel();
		buttons.add(search);
		buttons.add(confirm);
		buttons.add(cancel);

		center.add(buttons);

		this.add(west, BorderLayout.WEST);
		this.add(center, BorderLayout.CENTER);
		this.add(east, BorderLayout.EAST);

		SetUpTable();
	}

	public void SetUpTable(){
		Object[][] tableData = Controller.GetBookings(null, "UNCONFIRMED");

		CreateTable(tableData);
		informationTable.setVisible(false);
		informationTable.setVisible(true);
	}

	void CreateTable(Object[][] tableData){

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		if (informationTable == null)
		{
			informationTable = new JTable(tableData, columnNames){
				@Override 
				public boolean isCellEditable(int row, int column) { 
					return false; 
				} 
			};
			informationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		else
			informationTable.setModel(model);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(5, new IntegerComparator());

		informationTable.setRowSorter(sorter);

		informationTable.setPreferredScrollableViewportSize(new Dimension(700, 320));
		informationTable.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(informationTable);
		this.add(scrollPane, BorderLayout.SOUTH);
		informationTable.repaint();
	}

	private class ConfirmButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (informationTable.getSelectedRow() >= 0)
				Controller.ChangeBookingStatus(Controller.GetBookings(null, "UNCONFIRMED")[informationTable.getSelectedRow()][0], "CHECKED_IN");
			else
				JOptionPane.showMessageDialog(null, "Please Select A Guest.");
			new SearchButton().actionPerformed(null);
			//informationTable.validate();
			//informationTable.repaint();
			/*SetUpTable();
			Update();*/
		}
	}

	private class SearchButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
				DefaultTableModel model = new DefaultTableModel(Controller.GetBookings(null, "UNCONFIRMED"), columnNames);

				informationTable.setModel(model);

				RowFilter<Object, Object> filter = new RowFilter<Object, Object>()
				{
					public boolean include(Entry<?, ?> entry)
					{
						return (((0 == entry.getStringValue(0).compareTo(bidText.getText())) || bidText.getText().length() < 1)
								&& ((0 == entry.getStringValue(1).compareTo(firstNameText.getText())) || firstNameText.getText().length() < 1)
								&& ((0 == entry.getStringValue(2).compareTo(lastNameText.getText())) || lastNameText.getText().length() < 1)
								&& ((0 == entry.getStringValue(3).compareTo(emailText.getText())) || emailText.getText().length() < 1));
					}
				};

				TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);
				sorter.setRowFilter(filter);
				if (bidText.getText().length() < 1 
						&& firstNameText.getText().length() < 1
						&& lastNameText.getText().length() < 1
						&& emailText.getText().length() < 1)
					sorter.setRowFilter(null);
				informationTable.setRowSorter(sorter);

				//SetUpTable();
				//Update();
			
		}
	}

	private class CancelButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (informationTable.getSelectedRow() >= 0)
				Controller.CancelBooking(Controller.GetBookings(null, "UNCONFIRMED")[informationTable.getSelectedRow()][0]);
			else
				JOptionPane.showMessageDialog(null, "Please Select A Guest.");
			new SearchButton().actionPerformed(null);
		}
	}

	public void Update(){
		this.setVisible(false);
		this.setVisible(true);
	}

	public void UpdateTable(){
		new SearchButton().actionPerformed(null);
	}
}
